/**
 * Source Code Adapted From: 
 * http://java.sun.com/docs/books/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java
 */


package edu.tamu.cse.teamproject1.AirlineApplication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;



public class tableView extends JPanel{
	private boolean DEBUG = false;

	String[] columnNames = {};

    Object[][] data = {};
    
    public tableView(String[] newColumnNames, Object[][] newData) {
     	super(new GridLayout(1,0));
     	
     	columnNames = newColumnNames;
    	data = newData;
    	

        JTable table = new JTable(newData, newColumnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        /*if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }*/

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    /**
     * Creates the view and shows it on the screen
     */
    public void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        tableView newContentPane = new tableView(columnNames, data);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Default Close Operation = Hide Window so console continues to run
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
}


